﻿using System;
using System.Collections.Generic;
using Jlvier.TaobaoShowcase.Service;

namespace ShowWindowIntelligence
{
    internal class ServiceProvider
    {
        private static ServiceProvider instance;
        private static readonly object lockObject= new object();
        private Dictionary<Type, object> serviceContainer;

        public static ServiceProvider Instance
        {
            get
            {
                if (null == instance)
                {
                    lock(lockObject)
                    {
                        if (null == instance)
                            instance = new ServiceProvider();
                    }
                }
                return instance;
            }
        }

        private ServiceProvider()
        {
            BuildService();
        }

        private void BuildService()
        {
            serviceContainer = new Dictionary<Type, object>
                                   {
                                       {typeof (IProductItemService), new ProductItemService()}
                                   };
        }

        public T GetService<T>()
        {
            if (serviceContainer.ContainsKey(typeof(T)))
            {
                return (T) serviceContainer[typeof (T)];
            }
            throw new ArgumentNullException();
        }
    }
}
